package com.itboye.ihomebank.activity.finance;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.FinancialXiangQingBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.DoubleUtil;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by itboye on 2017/2/20.
 * <p>
 * 租金宝
 */

public class RentActivity extends BaseOtherActivity implements Observer {
    View v_statusbar;
    private TextView add_shap_title_tv;//标题
    private ImageView close_icon;//返回
    private LinearLayout linearTurOut;//转入
    private LinearLayout linearToChangInto;//转出
    TextView rent_zuori_shouyi, rent_zongjine, rent_nianhua;
    String id;
    String uid;
    JinRongPresenter presenter;
    FinancialXiangQingBean bean;

    @Override
    protected int layout_id() {
        return R.layout.activity_rent;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("租金宝");
        id = getIntent().getStringExtra("id");
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        presenter = new JinRongPresenter(this);
        showProgressDialog("获取数据,请稍后...", true);
        presenter.onXiangQing(id, uid);

        IntentFilter filter = new IntentFilter("zhuangzhang_ok");
        registerReceiver(shuxinBroadcastReceiver, filter);

        IntentFilter filter01 = new IntentFilter(FinanceActivity.DAOQI);
        registerReceiver(jieshuReceiver, filter01);
    }

    BroadcastReceiver jieshuReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
            ByAlert.alert("该商品不在购买时间内");
        };
    };

    BroadcastReceiver shuxinBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            presenter.onXiangQing(id, uid);
        };
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(shuxinBroadcastReceiver);
        unregisterReceiver(jieshuReceiver);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.close_icon:
                this.finish();
                break;
            case R.id.linearTurOut:
                intent = new Intent(RentActivity.this, ZhuanRuActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                break;
            case R.id.linearToChangInto:
                intent = new Intent(RentActivity.this, ZhuanChuActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("money", bean.getTotal());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == JinRongPresenter.financial_xq_success) {
                bean = (FinancialXiangQingBean) entity.getData();
                if (bean != null) {
                    rent_zuori_shouyi.setText("￥" + DoubleUtil.doubleTransform(bean.getInterest_yes()));
                    rent_zongjine.setText("￥" + DoubleUtil.doubleTransform(bean.getTotal()));
                    rent_nianhua.setText(DoubleUtil.doubleTransform(bean.getExpected_annualize()) + "%");
                }
            }
            if (entity.getEventType() == JinRongPresenter.financial_xq_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
